/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import View.EditFrm;
import com.mysql.jdbc.Connection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import model.Student;

/**
 *
 * @author hale
 */
public class EditCtr {

    private Connection con;
    private Student student;
    private EditFrm edit;
    Vector<Student> vtStudent = new Vector<>();

    public EditCtr() {
        edit = new EditFrm();
        String dbUrl = "jdbc:mysql://localhost:3306/ftico";
        String dbClass = "com.mysql.jdbc.Driver";
        try {
            Class.forName(dbClass);
            con = (Connection) DriverManager.getConnection(dbUrl, "root", "1234");
            System.out.println("ket noi rui");
            loadTable();
            loadTextField();
            edit.addUpdateListener(new ButtonUpdate());

        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            System.out.println("chua ket noi");
        } catch (SQLException ex) {
            Logger.getLogger(EditCtr.class.getName()).log(Level.SEVERE, null, ex);
        }

        edit.setVisible(true);
    }

    public void loadTextField() {
        int i = edit.getTblInfor().getSelectedRowCount();
        System.out.println(i);
        Student stu = vtStudent.get(i);
        edit.getTF2().setText(stu.getId());
        edit.getTF3().setText(stu.getName());
        edit.getTF4().setText(Integer.toString(stu.getAge()));
        edit.getTF5().setText(stu.getSex());
        edit.getTF6().setText(stu.getHomeTown());

    }

    class ButtonUpdate implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            //System.out.println(edit.getStudent().getHomeTown());
            editInfor();
        }
    }

    public void loadTable() {
        String sql = "SELECT * FROM students";
        try {
            Statement stm = con.createStatement();
            ResultSet rss = stm.executeQuery(sql);
            while (rss.next()) {
                String idStudents = rss.getString("idStudents");
                String nameStudent = rss.getString("nameStudent");
                int ageStudent = Integer.parseInt(rss.getString("ageStudent"));
                String sex = rss.getString("sexStudent");
                String homeTown = rss.getString("homeTown");
                vtStudent.add(new Student(idStudents, ageStudent, nameStudent, sex, homeTown));
            }
            DefaultTableModel tableModel = (DefaultTableModel) edit.getTblInfor().getModel();
            tableModel.getDataVector().removeAllElements();
            for (Student st : vtStudent) {
                tableModel.addRow(st.list());
            }
        } catch (SQLException ex) {
            Logger.getLogger(EditCtr.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void editInfor() {
        PreparedStatement ps = null;
        String sql = "UPDATE students SET  sexStudent=?,ageStudent=?,homeTown=?,nameStudent=? WHERE idStudents = ?";
        String sql1 = "INSERT INTO students(nameStudent,ageStudent,sexStudent,homeTown,idStudents) VALUES(?,?,?,?,?)";
        try {
            ps = con.prepareStatement(sql);
            String name = edit.getTF3().getText();
            //Integer age = Integer.parseInt(edit.getTF4().getText());
            String age = edit.getTF4().getText();
            String sex = edit.getTF5().getText();
            String homTown = edit.getTF6().getText();
            String id = edit.getTF2().getText();
            ps.setString(1, sex);
            ps.setString(2, age);
            ps.setString(3, homTown);
            ps.setString(4, name);
            ps.setString(5, id);

           // System.out.println(sql);
           // System.out.println(id + " " + name + " " + age + " " + sex + " " + homTown);
            ps.executeUpdate();
            System.out.println("Execute thanh cong");
        } catch (SQLException ex) {
            Logger.getLogger(EditCtr.class.getName()).log(Level.SEVERE, null, ex);
        }
        edit.showMess("Sucess");
    }
}
